//
//  MYGMedicalToolsCHAController.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/4/21.
//

import UIKit

class MYGMedicalToolsCHAController: MYGBaseController {
    // MARK: - Property
    private var H: Int = -1
    private var A: Int = -1
    private var S: Int = -1
    private var B: Int = -1
    private var L: Int = -1
    private var age: Int = -1
    private var sex: Int = -1

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        view.addSubviews([
            scrollView,
            commitButton
        ])
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(commitButton.snp.top)
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let HView = createChooseView(title: "慢性心衰", index: 0)
        let AView = createChooseView(title: "高血压", index: 1)
        let SView = createChooseView(title: "血管病", index: 2)
        let BView = createChooseView(title: "糖尿病", index: 3)
        let LView = createChooseView(title: "既往脑卒中或TIA", index: 4)
        let s1 = createSeparator()
        let s2 = createSeparator()
        scrollContentView.addSubviews([
            HView,
            AView,
            SView,
            BView,
            LView,
            ageInputView,
            s1,
            sexChooseView,
            s2,
            scoreView
        ])
        
        
        HView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        AView.snp.makeConstraints { make in
            make.top.equalTo(HView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        SView.snp.makeConstraints { make in
            make.top.equalTo(AView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        BView.snp.makeConstraints { make in
            make.top.equalTo(SView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        LView.snp.makeConstraints { make in
            make.top.equalTo(BView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        ageInputView.snp.makeConstraints { make in
            make.top.equalTo(LView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        s1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(ageInputView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        sexChooseView.snp.makeConstraints { make in
            make.top.equalTo(ageInputView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        s2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(sexChooseView.snp.bottom)
            make.height.equalTo(0.5)
        }
       
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(sexChooseView.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    
    func createChooseView(title: String, index:Int) -> MYGMedicalToolsChooseView {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = title
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {(ok) in
            if index == 0 {
                self.H = ok
            } else if index == 1 {
                self.A = ok
            } else if index == 2 {
                self.S = ok
            } else if index == 3 {
                self.B = ok
            } else if index == 4 {
                self.L = ok
            }
        }
        
        let separator = createSeparator()
        view.addSubview(separator)
        separator.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        return view
    }
    
    // MARK: - lazy load
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()

    private lazy var scrollContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
        
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "正常"
        view.title = "0"
        return view
    }()
    
    private lazy var ageInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .age, frame: .zero)
        view.inputDidChange = {[weak self] (age) in
            self?.age = Int(age) ?? 0
        }
        return view
    }()
    
    private lazy var sexChooseView: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "性别"
        view.leftTitle = "男"
        view.rightTitle = "女"
        view.selectedAction = {[weak self] (sex) in
            self?.sex = sex
        }
        return view
    }()
    
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
}

extension MYGMedicalToolsCHAController {
    @objc func commit() {
        view.endEditing(true)
        guard H != -1 && A != -1 && S != -1 && B != -1 && age != -1 && sex != -1 else { return }
        
        var score = ScoreValue(H).score + ScoreValue(A).score + ScoreValue(S).score + ScoreValue(B).score
        if sex == 1 {
            score = score + 1
        }
        
        if L == 0 { //脑卒中
            score = score + 2
        }
        
        if age >= 65 && age <=  74 {
            score = score + 1
        } else if age >= 75 {
            score = score + 2
        }
        if score < 2 {
            scoreView.level = "正常"
        } else if score < 6 {
            scoreView.level = "低危"
        } else {
            scoreView.level = "高危"
        }
        scoreView.title = "\(score)"
        scoreView.progress = Double(score) / 8.0
    }
}
